//
//  SecondController.m
//  Cook
//
//  Created by Macbookpro on 15/4/15.
//  Copyright (c) 2015年 dingjun. All rights reserved.
//

#import "SecondController.h"
#import "CollectionCellModel.h"
#import "ArrayModel.h"
#import "MenuController.h"

@interface SecondController ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    CGAdapter _myAd;
}
@property (nonatomic, retain) NSMutableArray *collectionArray;
@property (nonatomic, retain) NSMutableArray *photoArray;
@end

@implementation SecondController
- (void)dealloc
{
    [_collectionArray release];
    [_photoArray release];
    [_titleName release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionArray = [NSMutableArray array];
        self.photoArray = [NSMutableArray array];
        _myAd = [AdapterModel getCGAdapter];
        
        ArrayModel *model0 = [ArrayModel ArrayModelWithName0:@"茎叶类" name1:@"花菜类" name2:@"块根类" name3:@"瓜果类" name4:@"豆类" name5:@"豆制品" name6:@"菌菇类" name7:@"其他"];
        ArrayModel *model1 = [ArrayModel ArrayModelWithName0:@"猪肉" name1:@"牛肉" name2:@"羊肉" name3:@"鸡肉" name4:@"鸭肉" name5:@"鹅肉" name6:@"蛋类" name7:@"其他"];
        ArrayModel *model2 = [ArrayModel ArrayModelWithName0:@"淡水鱼" name1:@"海鱼" name2:@"虾" name3:@"蟹" name4:@"贝" name5:@"龟" name6:@"海藻类" name7:@"其他"];
        ArrayModel *model3 = [ArrayModel ArrayModelWithName0:@"素菜类" name1:@"畜肉类" name2:@"禽肉类" name3:@"炒水产" name4:@"炒海鲜" name5:@"炒饭" name6:@"炒面" name7:@"其他"];
        ArrayModel *model4 = [ArrayModel ArrayModelWithName0:@"素菜汤" name1:@"畜肉汤" name2:@"禽肉汤" name3:@"蛋汤" name4:@"水产汤" name5:@"海鲜汤" name6:@"甜品汤" name7:@"药膳汤"];
        ArrayModel *model5 = [ArrayModel ArrayModelWithName0:@"米饭" name1:@"粥" name2:@"包子馒头" name3:@"饺子" name4:@"馄饨云吞" name5:@"面条粉条" name6:@"小吃" name7:@"其他"];
        ArrayModel *model6 = [ArrayModel ArrayModelWithName0:@"川菜" name1:@"粤菜" name2:@"湘菜" name3:@"苏菜" name4:@"闽菜" name5:@"浙菜" name6:@"微菜" name7:@"鲁菜"];
        ArrayModel *model7 = [ArrayModel ArrayModelWithName0:@"中点" name1:@"西点" name2:@"甜点" name3:@"蛋糕" name4:@"面包" name5:@"西饼" name6:@"果冻" name7:@"其他"];
        ArrayModel *model8 = [ArrayModel ArrayModelWithName0:@"养颜蔬果汁" name1:@"瘦身蔬果汁" name2:@"保健蔬果汁" name3:@"开胃蔬果汁" name4:@"润肺蔬果汁" name5:@"清热蔬果汁" name6:@"降压蔬果汁" name7:@"其他"];
        ArrayModel *model9 = [ArrayModel ArrayModelWithName0:@"干果豆浆" name1:@"花草豆浆" name2:@"食疗豆浆" name3:@"蔬菜豆浆" name4:@"水果豆浆" name5:@"五谷豆浆" name6:@"原味豆浆" name7:@"其他"];
        [_collectionArray addObject:model0];
        [_collectionArray addObject:model1];
        [_collectionArray addObject:model2];
        [_collectionArray addObject:model3];
        [_collectionArray addObject:model4];
        [_collectionArray addObject:model5];
        [_collectionArray addObject:model6];
        [_collectionArray addObject:model7];
        [_collectionArray addObject:model8];
        [_collectionArray addObject:model9];
        
        ArrayModel *photo0 = [ArrayModel ArrayModelWithName0:@"jingye.png" name1:@"huacai.png" name2:@"kuaigen.png" name3:@"guaguo.png" name4:@"dou.png" name5:@"douzhipin.png" name6:@"jungu.png" name7:@"sucaiqita.png"];
        ArrayModel *photo1 = [ArrayModel ArrayModelWithName0:@"zhurou.png" name1:@"niurou.png" name2:@"yangrou.png" name3:@"jirou.png" name4:@"yarou.png" name5:@"erou.png" name6:@"dan.png" name7:@"huncaiqita"];
        ArrayModel *photo2 = [ArrayModel ArrayModelWithName0:@"danshuiyu.png" name1:@"haiyu.png" name2:@"xia.png" name3:@"xie.png" name4:@"bei.png" name5:@"gui.png" name6:@"haizao.png" name7:@"haixianqita.png"];
        ArrayModel *photo3 = [ArrayModel ArrayModelWithName0:@"shucai.png" name1:@"xurou.png" name2:@"qinrou.png" name3:@"chaoshuichan.png" name4:@"chaohaixian.png" name5:@"chaofan.png" name6:@"chaomian.png" name7:@"xiaochaoqita.png"];
        ArrayModel *photo4 = [ArrayModel ArrayModelWithName0:@"sucaitang.png" name1:@"xuroutang.png" name2:@"qinroutang.png" name3:@"dantang.png" name4:@"shuichantang.png" name5:@"haixiantang.png" name6:@"tianpintang.png" name7:@"yanshantang.png"];
        ArrayModel *photo5 = [ArrayModel ArrayModelWithName0:@"mifan.png" name1:@"zhou.png" name2:@"baozi.png" name3:@"jiaozi.png" name4:@"hundunyuntun.png" name5:@"miantiaofentiao.png" name6:@"xiaochi.png" name7:@"fushiqita.png"];
        ArrayModel *photo6 = [ArrayModel ArrayModelWithName0:@"chuan.png" name1:@"yue.png" name2:@"xiang.png" name3:@"su.png" name4:@"min.png" name5:@"zhe.png" name6:@"wei.png" name7:@"lu.png"];
        ArrayModel *photo7 = [ArrayModel ArrayModelWithName0:@"zhongdian.png" name1:@"xidian.png" name2:@"tiandian.png" name3:@"dangao.png" name4:@"mianbao.png" name5:@"xibing.png" name6:@"guodong.png" name7:@"qita.png"];
        ArrayModel *photo8 = [ArrayModel ArrayModelWithName0:@"yangyanshuguozhi.png" name1:@"shoushenshuguozhi.png" name2:@"baojianshuguozhi.png" name3:@"kaiweishuguozhi.png" name4:@"runfeishuguozhi.png" name5:@"qingreshuguozhi.png" name6:@"jiangyashuguozhi.png" name7:@"guozhiqita.png"];
        ArrayModel *photo9 = [ArrayModel ArrayModelWithName0:@"doujiang.png" name1:@"doujiang.png" name2:@"doujiang.png" name3:@"doujiang.png" name4:@"doujiang.png" name5:@"doujiang.png" name6:@"doujiang.png" name7:@"doujiang.png"];
        
        [_photoArray addObject:photo0];
        [_photoArray addObject:photo1];
        [_photoArray addObject:photo2];
        [_photoArray addObject:photo3];
        [_photoArray addObject:photo4];
        [_photoArray addObject:photo5];
        [_photoArray addObject:photo6];
        [_photoArray addObject:photo7];
        [_photoArray addObject:photo8];
        [_photoArray addObject:photo9];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    [self creatBackGround];
    [self creatCollection];
    [self creatNavigationBar];
}

- (void)creatBackGround
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
    [imageView setImage:[UIImage imageNamed:@"classifyPhoto1.png"]];
    [self.view addSubview:imageView];
    [imageView release];
    
}

- (void)creatNavigationBar
{
    self.navigationItem.title = self.titleName;
    
    UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [aButton setTitle:@"返回" forState:UIControlStateNormal];
    [aButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [aButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    [aButton addTarget:self action:@selector(aButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [aButton release];
    [self.navigationItem.leftBarButtonItem release];

}

- (void)aButtonAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)creatCollection
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(100 * _myAd.sWidth, 130 * _myAd.sHeight);
//    flow.minimumInteritemSpacing = 55.0f;
    flow.minimumLineSpacing = 10.0f;
    flow.sectionInset = UIEdgeInsetsMake(20, 50, 20, 50);
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 * _myAd.sHeight, self.view.frame.size.width, self.view.frame.size.height - 64 * _myAd.sHeight) collectionViewLayout:flow];
    [flow release];
    collection.backgroundColor = [UIColor clearColor];
    

    [collection registerClass:[CollectionCellModel class] forCellWithReuseIdentifier:@"cell"];
    collection.dataSource = self;
    collection.delegate = self;
    collection.scrollEnabled = NO;
    [self.view addSubview:collection];
    [collection release];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionCellModel *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    ArrayModel *model = [_collectionArray objectAtIndex:self.arrayNum];
    cell.label.backgroundColor = [UIColor clearColor];
    cell.label.textColor = [UIColor whiteColor];
    
    switch (indexPath.item) {
        case 0:
        {
            cell.label.text = model.name0;
        }
            break;
            case 1: 
        {
            cell.label.text = model.name1;
        }
            break;
        case 2:
        {
            cell.label.text = model.name2;
        }
            break;
        case 3:
        {
            cell.label.text = model.name3;
        }
            break;
        case 4:
        {
            cell.label.text = model.name4;
        }
            break;
        case 5:
        {
            cell.label.text = model.name5;
        }
            break;
        case 6:
        {
            cell.label.text = model.name6;
        }
            break;
        case 7:
        {
            cell.label.text = model.name7;
        }
            break;
            
        default:
            break;
    }
    
    ArrayModel *photo = [_photoArray objectAtIndex:self.arrayNum];
    switch (indexPath.item) {
        case 0:
        {
            [cell.imageView setImage:[UIImage imageNamed:photo.name0]];
        }
            break;
        case 1:
        {
            [cell.imageView setImage:[UIImage imageNamed:photo.name1]];
        }
            break;
        case 2:
        {
            [cell.imageView setImage:[UIImage imageNamed:photo.name2]];
        }
            break;
        case 3:
        {
            [cell.imageView setImage:[UIImage imageNamed:photo.name3]];
        }
            break;
        case 4:
        {
            [cell.imageView setImage:[UIImage imageNamed:photo.name4]];
        }
            break;
        case 5:
        {
            [cell.imageView setImage:[UIImage imageNamed:photo.name5]];
        }
            break;
        case 6:
        {
            [cell.imageView setImage:[UIImage imageNamed:photo.name6]];
        }
            break;
        case 7:
        {
            [cell.imageView setImage:[UIImage imageNamed:photo.name7]];
        }
            break;
            
        default:
            break;
    }

    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MenuController *menu = [[MenuController alloc] init];
    if (_arrayNum >= 0 && _arrayNum < 4) {
        menu.idNumber = 84 + 8 * _arrayNum + indexPath.item;
    } else if (_arrayNum >= 4 && _arrayNum < 7) {
        menu.idNumber = 125 + 8 * (_arrayNum - 4) + indexPath.item;
    } else {
        menu.idNumber = 150 + 8 * (_arrayNum - 7) + indexPath.item;
    }
    menu.title1 = _titleName;
    
    ArrayModel *model = [_collectionArray objectAtIndex:self.arrayNum];
    switch (indexPath.item) {
        case 0:
        {
            menu.title2 = model.name0;
        }
            break;
        case 1:
        {
            menu.title2 = model.name1;
        }
            break;
        case 2:
        {
            menu.title2 = model.name2;
        }
            break;
        case 3:
        {
            menu.title2 = model.name3;
        }
            break;
        case 4:
        {
            menu.title2 = model.name4;
        }
            break;
        case 5:
        {
            menu.title2 = model.name5;
        }
            break;
        case 6:
        {
            menu.title2 = model.name6;
        }
            break;
        case 7:
        {
            menu.title2 = model.name7;
        }
            break;
            
        default:
            break;
    }

    [self.navigationController pushViewController:menu animated:YES];
    [menu release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
